package com.kgis.map.control.infowindow
{
    import com.kgis.map.Map;
    import com.kgis.map.events.MapEvent;
    import com.kgis.map.geometry.basetype.Location;
    import com.kgis.map.geometry.basetype.Pixel;
    
    import flash.events.*;
    import flash.geom.*;
    
    import mx.core.*;
    import mx.events.*;
    
    import spark.components.supportClasses.*;

    public class InfoWindow extends InfoComponent
    {
        private var m_stageX:Number;
        private var m_stageY:Number;
        private var m_contentChanged:Boolean = false;
        private var m_labelVisible:Boolean = true;
        private var m_closeButtonVisible:Boolean = true;
        private var m_infoWindowHeaderVisible:Boolean = true;
        private var m_content:UIComponent;
        private var m_draggableContent:IEventDispatcher;
        public var infoPlacement:String;
        public var isMouseDown:Boolean = false;
        public var deltaX:Number;
        public var deltaY:Number;
        public var containerGroup:GroupBase;
        public var headerGroup:GroupBase;
        public var labelText:TextBase;
        public var closeButton:ButtonBase;
        public var contentOwner:Object;
        private var m_label:String;
        private var m_labelChanged:Boolean = false;
        private var m_mapPointX:Number;
        private var m_mapPointY:Number;
        private var m_mapPoint:Location;
        private var m_anchorX:Number;
        private var m_anchorY:Number;
        private static var _skinParts:Object = {headerGroup:false, labelText:false, contentGroup:false, closeButton:false, containerGroup:false};

        public function InfoWindow(map:Map)
        {
            super(map);
            var _loc_2:Boolean = false;
            visible = false;
            includeInLayout = _loc_2;
            addEventListener("infoWindowLabelChanged", this.labelChangedHandler);
            return;
        }

        public function get label() : String
        {
            return this.m_label;
        }

        public  function set label(value:String) : void
        {
            if (this.m_label !== value)
            {
                this.m_label = value;
                this.m_labelChanged = true;
                invalidateProperties();
            }
            return;
        }

        public function get content() : UIComponent
        {
            return this.m_content;
        }

        public function set content(value:UIComponent) : void
        {
            var _loc_2:IInfoWindowContent = null;
            if (this.m_content !== value)
            {
                if (this.m_content)
                {
                    this.m_content.removeEventListener(Event.CLOSE, this.infoWindowContent_closeHandler);
                }
                _loc_2 = value as IInfoWindowContent;
                if (_loc_2)
                {
                }
                if (_loc_2.showInfoWindowHeader === false)
                {
                    this.m_infoWindowHeaderVisible = false;
                    value.addEventListener(Event.CLOSE, this.infoWindowContent_closeHandler);
                }
                else
                {
                    this.m_infoWindowHeaderVisible = true;
                }
                this.m_content = value;
                this.m_contentChanged = true;
                invalidateProperties();
                invalidateSize();
                invalidateDisplayList();
                invalidateSkinState();
                dispatchEvent(new Event("contentChange"));
            }
            return;
        }

        private function infoWindowContent_closeHandler(event:Event) : void
        {
            this.hide2(false);
            return;
        }

        public function get labelVisible() : Boolean
        {
            return this.m_labelVisible;
        }

        public function set labelVisible(value:Boolean) : void
        {
            this.m_labelVisible = value;
            invalidateProperties();
            invalidateSkinState();
            return;
        }

        public function get closeButtonVisible() : Boolean
        {
            return this.m_closeButtonVisible;
        }

        public function set closeButtonVisible(value:Boolean) : void
        {
            this.m_closeButtonVisible = value;
            invalidateProperties();
            invalidateSkinState();
            return;
        }

        override protected function partAdded(partName:String, instance:Object) : void
        {
            super.partAdded(partName, instance);
            if (instance === this.closeButton)
            {
                this.closeButton.addEventListener(MouseEvent.CLICK, this.closeButton_clickHandler);
            }
            else
            {
                if (instance === contentGroup)
                {
                }
                if (this.m_contentChanged)
                {
                    this.m_contentChanged = false;
                    if (this.m_content)
                    {
                        contentGroup.addElement(this.m_content);
                    }
                }
                else if (instance === this.headerGroup)
                {
                    this.headerGroup.addEventListener(MouseEvent.MOUSE_DOWN, this.headerGroup_mouseDownHandler);
                }
            }
            return;
        }

        override protected function partRemoved(partName:String, instance:Object) : void
        {
            super.partRemoved(partName, instance);
            if (instance === this.closeButton)
            {
                this.closeButton.removeEventListener(MouseEvent.CLICK, this.closeButton_clickHandler);
            }
            else if (instance === this.headerGroup)
            {
                this.headerGroup.removeEventListener(MouseEvent.MOUSE_DOWN, this.headerGroup_mouseDownHandler);
            }
            return;
        }

        private function draggableContent_mouseDownHandler(event:MouseEvent) : void
        {
            if (event.target === this.m_draggableContent)
            {
            }
            if (this.containerGroup)
            {
                this.addStageMouseDownMoveListeners(event);
            }
            return;
        }

        private function headerGroup_mouseDownHandler(event:MouseEvent) : void
        {
            if (event.target === this.headerGroup)
            {
            }
            if (this.containerGroup)
            {
                this.addStageMouseDownMoveListeners(event);
            }
            return;
        }

        private function addStageMouseDownMoveListeners(event:MouseEvent) : void
        {
            this.deltaX = 0;
            this.deltaY = 0;
            this.isMouseDown = true;
            this.m_stageX = event.stageX;
            this.m_stageY = event.stageY;
            event.stopImmediatePropagation();
//            map.mapNavigationEnabled = false;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.stage_mouseUpHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMoveHandler);
            return;
        }

        private function stage_mouseUpHandler(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.stage_mouseUpHandler);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMoveHandler);
            this.isMouseDown = false;
            this.deltaX = 0;
            this.deltaY = 0;
//            map.mapNavigationEnabled = true;
            var _loc_2:* = map.globalToLocal(this.containerGroup.localToGlobal(new Point(0, 0)));
            var _loc_3:* = map.globalToLocal(this.containerGroup.localToGlobal(new Point(this.containerGroup.width, this.containerGroup.height)));
            if (anchorY > _loc_3.y)
            {
                if (anchorX < _loc_2.x)
                {
                    this.infoPlacement = InfoPlacement.UPPERRIGHT;
                }
                else if (anchorX < _loc_3.x)
                {
                    this.infoPlacement = InfoPlacement.TOP;
                }
                else
                {
                    this.infoPlacement = InfoPlacement.UPPERLEFT;
                }
            }
            else if (anchorY < _loc_2.y)
            {
                if (anchorX < _loc_2.x)
                {
                    this.infoPlacement = InfoPlacement.LOWERRIGHT;
                }
                else if (anchorX < _loc_3.x)
                {
                    this.infoPlacement = InfoPlacement.BOTTOM;
                }
                else
                {
                    this.infoPlacement = InfoPlacement.LOWERLEFT;
                }
            }
            else if (anchorX < _loc_2.x)
            {
                this.infoPlacement = InfoPlacement.RIGHT;
            }
            else if (_loc_3.x < anchorX)
            {
                this.infoPlacement = InfoPlacement.LEFT;
            }
            else
            {
                this.infoPlacement = InfoPlacement.CENTER;
            }
            skin.invalidateSize();
            skin.invalidateDisplayList();
            return;
        }

        private function stage_mouseMoveHandler(event:MouseEvent) : void
        {
            this.deltaX = event.stageX - this.m_stageX;
            this.deltaY = event.stageY - this.m_stageY;
            this.m_stageX = event.stageX;
            this.m_stageY = event.stageY;
            event.stopImmediatePropagation();
            event.updateAfterEvent();
            skin.invalidateSize();
            skin.invalidateDisplayList();
            return;
        }

        private function closeButton_clickHandler(event:Event) : void
        {
            if (event.target === this.closeButton)
            {
                this.hide();
            }
            return;
        }

        private function labelChangedHandler(event:Event) : void
        {
            invalidateProperties();
            return;
        }

        override protected function getCurrentSkinState() : String
        {
            if (this.m_infoWindowHeaderVisible === false)
            {
                return "withoutHeader";
            }
            if (!this.m_labelVisible)
            {
            }
            return this.m_closeButtonVisible ? ("withHeader") : ("withoutHeader");
        }

        override protected function commitProperties() : void
        {
            var _loc_1:IInfoWindowDraggableContent = null;
            var _loc_2:IDataRenderer = null;
            super.commitProperties();
            if (this.m_contentChanged)
            {
            }
            if (contentGroup)
            {
                this.m_contentChanged = false;
                while (contentGroup.numElements)
                {
                    
                    contentGroup.removeElementAt(0);
                }
                if (this.m_content)
                {
                    _loc_2 = this.m_content as IDataRenderer;
                    if (data)
                    {
                    }
                    if (_loc_2)
                    {
                    }
                    if (_loc_2.data === null)
                    {
                        _loc_2.data = data;
                    }
                    contentGroup.addElement(this.m_content);
                }
                if (this.m_draggableContent)
                {
                    this.m_draggableContent.removeEventListener(MouseEvent.MOUSE_DOWN, this.draggableContent_mouseDownHandler);
                    this.m_draggableContent = null;
                    if (this.headerGroup)
                    {
                        this.headerGroup.removeEventListener(MouseEvent.MOUSE_DOWN, this.headerGroup_mouseDownHandler);
                        this.headerGroup.addEventListener(MouseEvent.MOUSE_DOWN, this.headerGroup_mouseDownHandler);
                    }
                }
                _loc_1 = this.m_content as IInfoWindowDraggableContent;
                if (_loc_1)
                {
                    this.m_draggableContent = _loc_1.draggableContent;
                    if (this.m_draggableContent)
                    {
                        this.m_draggableContent.addEventListener(MouseEvent.MOUSE_DOWN, this.draggableContent_mouseDownHandler);
                        if (this.headerGroup)
                        {
                            this.headerGroup.removeEventListener(MouseEvent.MOUSE_DOWN, this.headerGroup_mouseDownHandler);
                        }
                    }
                }
            }
            if (this.labelText)
            {
                if (this.m_labelChanged)
                {
                    this.m_labelChanged = false;
                    this.labelText.text = this.m_label;
                }
                if (this.m_label === null)
                {
                    if (this.m_content)
                    {
                    }
                    if (this.m_content.hasOwnProperty("label"))
                    {
                        this.labelText.text = this.m_content["label"];
                    }
                    else
                    {
                        if (this.m_content)
                        {
                        }
                        if (this.m_content.hasOwnProperty("infoWindowLabel"))
                        {
                            this.labelText.text = this.m_content["infoWindowLabel"];
                        }
                        else if (this.labelText.text)
                        {
                            this.labelText.text = "";
                        }
                    }
                }
                var _loc_3:* = this.labelVisible;
                this.labelText.includeInLayout = this.labelVisible;
                this.labelText.visible = _loc_3;
            }
            if (this.closeButton)
            {
                var _loc_3:* = this.closeButtonVisible;
                this.closeButton.includeInLayout = this.closeButtonVisible;
                this.closeButton.visible = _loc_3;
            }
            return;
        }
/**
 * @param mapPoint 地理坐标
 * 
 * */
        public function show(mapPoint:Location, stagePoint:Point = null) : void
        {
            var _loc_3:Pixel = null;
            includeInLayout = true;
            this.infoPlacement = null;
            this.m_mapPoint = mapPoint;
			
			this.m_mapPointX = mapPoint.x;
			this.m_mapPointY = mapPoint.y;
			_loc_3=m_map.getMapPxFromLocation(new Location(this.m_mapPointX, this.m_mapPointY));
			this.showXY(_loc_3.x, _loc_3.y);
           
        }

//        private function extentChangeHandler(event:ExtentEvent) : void
//        {
//            m_map.removeEventListener(ExtentEvent.EXTENT_CHANGE, this.extentChangeHandler);
//            if (m_map.wrapAround180)
//            {
//                this.getDenormalizedPoint();
//            }
//            var _loc_2:* = m_map.viewport.state.toScreenXY(this.m_mapPointX, this.m_mapPointY);
//            this.showXY(_loc_2.x, _loc_2.y);
//            return;
//        }

//        private function getDenormalizedPoint(stagePoint:Point = null) : void
//        {
//            var _loc_2:Location = null;
//            if (includeInLayout)
//            {
//                _loc_2 = GeomUtils.denormalizePoint(map, this.m_mapPoint, stagePoint);
//                if (_loc_2)
//                {
//                    visible = true;
//                    this.m_mapPointX = _loc_2.x;
//                    this.m_mapPointY = _loc_2.y;
//                }
//                else
//                {
//                    visible = false;
//                }
//            }
//            return;
//        }

        public function hide() : void
        {
            this.hide2(true);
            return;
        }

        function hide2(dispatchCloseEvent:Boolean = true) : void
        {
            if (!includeInLayout)
            {
            }
            if (visible)
            {
                var _loc_2:Boolean = false;
                visible = false;
                includeInLayout = _loc_2;
                this.removeMapListeners();
                if (dispatchCloseEvent)
                {
                    dispatchEvent(new Event(Event.CLOSE));
                }
            }
            return;
        }

        function showXY(pixelX:Number, pixelY:Number) : void
        {
			super.anchorX = pixelX;
			super.anchorY = pixelY;
            visible = true;
            this.addMapListeners();
            dispatchEvent(new Event(Event.OPEN));
            return;
        }

		private function addMapListeners():void
        {
            this.removeMapListeners();
            m_map.addEventListener(MapEvent.MOVE_END, this.map_zoomUpdateHandler);
            m_map.addEventListener(MapEvent.RESIZE, this.map_extentChangeHandler);
            return;
        }

        private function removeMapListeners() : void
        {
			m_map.removeEventListener(MapEvent.MOVE_END, this.map_zoomUpdateHandler);
			m_map.removeEventListener(MapEvent.RESIZE, this.map_extentChangeHandler);
            return;
        }

        private function map_extentChangeHandler(event:MapEvent) : void
        {
            var _loc_2:Pixel = m_map.getMapPxFromLocation(new Location(this.m_mapPointX, this.m_mapPointY));
			super.anchorX = _loc_2.x;
			super.anchorY = _loc_2.y;
            return;
        }

        private function map_zoomUpdateHandler(event:Event) : void
        {
            var _loc_2:* = m_map.getMapPxFromLocation(new Location(this.m_mapPointX, this.m_mapPointY));
			super.anchorX = _loc_2.x;
			super.anchorY = _loc_2.y;
            return;
        }

        override protected function get skinParts() : Object
        {
            return _skinParts;
        }

    }
}
